package pro.lepers.portfolio.view.contents.portfolio {
	import pro.lepers.portfolio.view.misc.cursor.CursorType;
	import pro.lepers.portfolio.view.misc.cursor.Cursor;
	import flash.events.MouseEvent;
	import pro.lepers.portfolio.Site;
	import caurina.transitions.Equations;
	import caurina.transitions.Tweener;

	import fr.digitas.flowearth.command.Batcher;
	import fr.digitas.flowearth.command.IBatchable;
	import fr.digitas.flowearth.core.IIterator;
	import fr.digitas.flowearth.event.NodeEvent;

	import pro.lepers.portfolio.view.contents.portfolio.data.contents.AbstractContentData;
	import pro.lepers.portfolio.view.contents.portfolio.data.contents.ProjectContentProvider;
	import pro.lepers.portfolio.view.contents.portfolio.thumb.ContentHolder;
	import pro.lepers.portfolio.view.contents.portfolio.thumb.Holder;

	import flash.display.Sprite;
	import flash.utils.Dictionary;

	/**
	 * @author Pierre Lepers
	 */
	public class ProjectContent extends Sprite {
		
		public function ProjectContent( pcp : ProjectContentProvider ) {
			_provider = pcp;
			_build();
			
			_provider.rootNode.addEventListener(NodeEvent.CHILD_CHANGE, onChildChange );
		}

		public function get mainThumb() : ContentHolder {
			return _mainThumb;
		}

		public function dispose () : void {
			_provider.rootNode.removeEventListener(NodeEvent.CHILD_CHANGE, onChildChange, true );
			_provider = null;

			_dthumbs = null;
			
			var holder : ContentHolder;
			for (var i : int = 0; i < _thumbs.length; i++) {
				holder = _thumbs[i];
				_thumbs[i] = null;
				Tweener.removeTweens( holder );
				holder.dispose();
			}
			_thumbs = null;
			_currentHolder = _mainThumb = null;
			
			stopOnBgClick ();
		}

		public function getMainLoadable() : IBatchable {
			return _mainThumb.content.getLoadables();
		}

		public function getLoadables() : IBatchable {
			return _loadables;
		}

		
		
		
		public function openHolder ( holder : ContentHolder ) : void {
			
			var newIndex : int = _thumbs.indexOf( holder );
			
			if( holder.parent )
				setChildIndex( holder, numChildren - 1 );
			else
				addChild( holder );
			
			var rx : Number;
			var ry : Number;
			var rot : Number;
			
			var hIndex : int;
					
			if( _currentHolder ) {
				
				hIndex = _thumbs.indexOf( _currentHolder );
				if( hIndex < newIndex )
					rx = - Math.random() * 400 - 300;
				else
					rx = Math.random() * 400 + 300;
					
				ry = Math.random() * 200 - 100;
				rot = Math.random() * 60 - 30;
				
				_currentHolder.locked = false;
				_currentHolder.unfocus();
				Tweener.addTween( _currentHolder, { x : rx, time : .8, rotation : rot, altitude: 1, transition : Equations.easeOutExpo, delay : 0 } );
				Tweener.addTween( _currentHolder, { y : ry, time : .8, transition : Equations.easeOutQuart, delay : 0} );
				Tweener.addTween( _currentHolder, { width : ProjectItem.THW*ProjectItem.COLLAPSE_SCALE, 
													height : ProjectItem.THH*ProjectItem.COLLAPSE_SCALE, 
													time : .3, 
													transition : Equations.easeOutQuad,
													delay : 0,
													onComplete : openOtherComplete, 
													onCompleteParams : [ _currentHolder ]
												} );
						
				
			} else {
//					_opened = true;
					
					var c : int = 0;
					
					var delay : Number;
					
					
					var thumb : Holder;
					
					for (var i : int = 0; i < _thumbs.length; i++) {
						
						thumb = _thumbs[ i ];
						if( thumb == holder ) continue;
						Tweener.removeTweens( thumb );
//						
//						thumb.y = ProjectItem.THH*ProjectItem.COLLAPSE_SCALE;
						
						
						hIndex = _thumbs.indexOf( thumb );
						if( hIndex < newIndex )
							rx = -Math.random() * 400 - 300;
						else
							rx = Math.random() * 400 + 300;
							
						ry = Math.random() * 200 - 100;
						rot = Math.random() * 60 - 30;
						
						delay = c * .1;
						
						thumb.locked = false;
						
						Tweener.addTween( thumb, { 
													x : rx, 
													time : .8, 
													rotation : rot, 
													altitude: 1, 
													transition : Equations.easeOutQuart, 
													delay : delay 
												} );
						Tweener.addTween( thumb, { 
													y : ry, 
													time : .8, 
													transition : Equations.easeOutQuad, 
													delay : delay, 
													onStart : onThumbOpenStart, 
													onStartParams : [ thumb ],
													onComplete : openOtherComplete, 
													onCompleteParams : [ thumb ] 
												} );
						
						c++;
				}
			}
			
			_currentHolder = holder;
			_currentHolder.locked = false;
			_currentHolder.focus();
			
			Tweener.addTween( holder , { 	width : ProjectItem.THW, 
											height : ProjectItem.THH, 
											x : 0,
											y : 0,
											rotation : 0,
											time : .3, 
											transition : ProjectItem.OPEN_EASE,
											onComplete : openCurrentComplete, 
											onCompleteParams : [ holder ]
										} );
			
		}

		public function close ( ) : void {
			if( !_currentHolder ) return;
//			_opened = false;
			
			var c : int = 0;
			var delay : Number;
			var thumb : ContentHolder;
			
			for (var i : int = 0; i < _thumbs.length; i++) {
				thumb = _thumbs[ i ];
				Tweener.removeTweens( thumb );
				if( thumb == _currentHolder ) continue;
				if( thumb.parent == null ) continue;
				Tweener.removeTweens( thumb );
				
				delay = c * .1;
				
				thumb.locked = false;
				
				Tweener.addTween( thumb, { x : 0, time : .2, rotation : 0,transition : Equations.easeInQuad, delay : delay } );
				Tweener.addTween( thumb, { y : 0, time : .2, altitude : 0, transition : Equations.easeOutQuad, delay : delay, onComplete : onThumbCloseComplete, onCompleteParams : [ thumb ] } );
				
				c++;
			}
			
			_currentHolder.locked = false;
			_currentHolder.unfocus();
			
			Tweener.addTween( _currentHolder , { 	width : ProjectItem.THW*ProjectItem.COLLAPSE_SCALE, 
													height : ProjectItem.THH*ProjectItem.COLLAPSE_SCALE, 
													x : 0, 
													time : ProjectItem.CLOSE_EASE_TIME, 
													transition : ProjectItem.CLOSE_EASE, 
													onComplete : closeComplete,
													onCompleteParams : [ _currentHolder]
											} );
			
			_currentHolder = null;
		}
		
		private function openCurrentComplete ( holder : ContentHolder ) : void {
			holder.locked = true;
			holder.focused();
		}
		
		private function openOtherComplete ( holder : ContentHolder ) : void {
			holder.locked = true;
			holder.unfocused();
		}

		private function closeComplete ( holder : ContentHolder ) : void {
			holder.locked = true;
			holder.unfocused();
		}

		private function onThumbOpenStart ( holder : ContentHolder ) : void {
			addChildAt( holder, numChildren - 1 );
			holder.shown();
		}
		
		private function onThumbCloseComplete ( holder : ContentHolder ) : void {
			holder.hidded();
			holder.unfocused();
			removeChild( holder );
		}
		
		
		
		private function onChildChange (event : NodeEvent) : void {
			var data : AbstractContentData = _provider.getCurrentContent();
			if( data ) {
				openHolder( _dthumbs[ data ] );
				closeOnBgClick();
			} else {
				stopOnBgClick();
				close();
			}
		}

		private function closeOnBgClick () : void {
			Site.background.addEventListener(MouseEvent.MOUSE_DOWN, onDownBg );
			Site.background.addEventListener(MouseEvent.MOUSE_OVER, onOverBg );
			Site.background.addEventListener(MouseEvent.MOUSE_OUT, onOutBg );
		}

		private function stopOnBgClick () : void {
			Site.background.removeEventListener(MouseEvent.MOUSE_DOWN, onDownBg );
			Site.background.removeEventListener(MouseEvent.MOUSE_OVER, onOverBg );
			Site.background.removeEventListener(MouseEvent.MOUSE_OUT, onOutBg );
			Cursor.removeCursor( CursorType.CLOSE );
		}

		private function onDownBg (event : MouseEvent) : void {
			_provider.rootNode.activate();
		}

		private function onOverBg (event : MouseEvent) : void {
			Cursor.setCursor( CursorType.CLOSE );
		}

		private function onOutBg (event : MouseEvent) : void {
			Cursor.removeCursor( CursorType.CLOSE );
		}

		
		private function _build () : void {
			
			var w : int = ProjectItem.THW * ProjectItem.COLLAPSE_SCALE;
			var h : int = ProjectItem.THH*ProjectItem.COLLAPSE_SCALE;
			
		
			
			
//			var toto : Shape = new Shape();
//			addChild( toto );
//			toto.graphics.beginFill( 0 );
//			toto.graphics.drawRect(0, 0, 10, 10);
			
			
			_dthumbs = new Dictionary();
			
			_thumbs = new Vector.<ContentHolder>( _provider.length );
			
			_loadables = new Batcher();
			
			var iter : IIterator = _provider.getContents();
			var item : AbstractContentData;
			var thumb : ContentHolder;
			var loadables : IBatchable;
			var c : int = 0;
			while( iter.hasNext() ) {
				item = iter.next() as AbstractContentData;
				thumb = new ContentHolder( item, _provider.rootNode.getChild(item.getId() ), w, h );
				_thumbs[c] = thumb;
				
				if( c > 0 && (loadables = thumb.content.getLoadables()) ) 
					_loadables.addItem( loadables );
				_dthumbs[ item ] = thumb;
				c ++;
			}
			_mainThumb = _thumbs[0];
			addChild( _mainThumb );
			_mainThumb.shown();
		}

		private var _loadables : Batcher;

		private var _mainThumb : ContentHolder;
		
		private var _currentHolder : ContentHolder;

		
		private var _thumbs :  Vector.<ContentHolder>;
		private var _dthumbs : Dictionary;

		private var _provider : ProjectContentProvider;
		
//		private var _opened : Boolean = false;

	}
}
